import type { RouteRecordRaw } from "vue-router"
import { routerConfig } from "@/router/config"
import { registerNavigationGuard } from "@/router/guard"
import { createRouter } from "vue-router"
import { flatMultiLevelRoutes } from "./helper"

const Layouts = () => import("@/layouts/index.vue")

/**
 * @name 常驻路由
 * @description 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置唯一的 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: ":path(.*)",
        component: () => import("@/pages/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/pages/error/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/pages/error/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/pages/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/Dashboard.vue"),
        name: "Dashboard",
        meta: {
          title: "首页仪表盘",
          svgIcon: "dashboard",
          affix: true
        }
      }
    ]
  },
  {
    path: '/assets',
    name: 'AssetsManage',
    component: Layouts,
    meta: { title: '设备与资产管理', svgIcon: 'assets' },
    children: [
      {
        path: 'device-group',
        name: 'DeviceGroupManage',
        component: () => import('@/views/assets/DeviceGroupManage.vue'),
        meta: { title: '设备分组管理' }
      },
      {
        path: 'device-list',
        name: 'DeviceListManage',
        component: () => import('@/views/assets/DeviceList.vue'),
        meta: { title: '设备信息管理' }
      },
      {
        path: 'product-list',
        name: 'ProductListManage',
        component: () => import('@/views/assets/ProductList.vue'),
        meta: { title: '产品与工装夹具管理' }
      }
    ]
  },
  {
    path: '/inventory',
    name: 'InventoryManage',
    component: Layouts,
    meta: { title: '物资与库存管理', svgIcon: 'inventory' },
    children: [
      {
        path: 'material',
        name: 'MaterialManage',
        component: () => import('@/views/inventory/MaterialManage.vue'),
        meta: { title: '低值耗材/配件管理' }
      },
      {
        path: 'warehouse',
        name: 'WarehouseManage',
        component: () => import('@/views/inventory/WarehouseManage.vue'),
        meta: { title: '多库房/二级库管理' }
      }
    ]
  },
  {
    path: '/maintenance',
    name: 'MaintenanceManage',
    component: Layouts,
    meta: { title: '运维与告警管理', svgIcon: 'maintenance' },
    children: [
      {
        path: 'maintenance',
        name: 'DeviceMaintenance',
        component: () => import('@/views/maintenance/DeviceMaintenance.vue'),
        meta: { title: '设备保养维护' }
      },
      {
        path: 'alarm',
        name: 'AlarmCenter',
        component: () => import('@/views/maintenance/AlarmCenter.vue'),
        meta: { title: '告警中心' }
      }
    ]
  },
  {
    path: '/report',
    name: 'ReportAnalysis',
    component: Layouts, 
    meta: { title: '统计分析', svgIcon: 'report' },
    children: [
      {
        path: 'device',
        name: 'DeviceQuery',
        component: () => import('@/views/report/DeviceQuery.vue'),
        meta: { title: '设备统计' }
      },
      {
        path: 'material',
        name: 'MaterialQuery',
        component: () => import('@/views/report/MaterialQuery.vue'),
        meta: { title: '物资统计' }
      },
      {
        path: 'alarm',
        name: 'AlarmQuery',
        component: () => import('@/views/report/AlarmQuery.vue'),
        meta: { title: '告警统计' }
      }
    ]
  }






]

/**
 * @name 动态路由
 * @description 用来放置有权限 (Roles 属性) 的路由
 * @description 必须带有唯一的 Name 属性
 */
export const dynamicRoutes: RouteRecordRaw[] = [

]

/** 路由实例 */
export const router = createRouter({
  history: routerConfig.history,
  routes: routerConfig.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  try {
    // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    location.reload()
  }
}

// 注册路由导航守卫
registerNavigationGuard(router)
